package com.example.redismanual.service.impl;


import com.example.redismanual.service.SearchHelpService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

@Service
public class SearchHelpServiceImpl implements SearchHelpService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Set<String> getHistorySearchKeyword(String userId) {
        return stringRedisTemplate.opsForSet().members(userId);
    }

    @Override
    public long cacheUserSearchKeyword(String userId, String keyword) {
        return stringRedisTemplate.opsForSet().add(userId, keyword);
    }

    @Override
    public Boolean deleteUserSearchKeyword(String userId) {
        return stringRedisTemplate.delete(userId);
    }

    @Override
    public double cacheAllSearchKeyword(String keyword) {
        if(stringRedisTemplate.opsForZSet().score("huoshui_keyword_zset", keyword) == null){
            stringRedisTemplate.opsForZSet().add("huoshui_keyword_zset",keyword,1);
            return 1;
        }else {
           return stringRedisTemplate.opsForZSet().incrementScore("huoshui_keyword_zset",keyword,1);
        }
    }

    @Override
    public Set<String> getHotestSearchKeyword() {
        if(stringRedisTemplate.opsForZSet().size("huoshui_keyword_zset") == 0){
            return null;
        }else {
            return stringRedisTemplate.opsForZSet().reverseRange("huoshui_keyword_zset",0,9);
        }

    }
}
